/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.xiao.service.impl;

import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.ejweixin.domain.NgCaseInfo;
import me.zhengjie.modules.ejweixin.domain.NgConstructionSiteInfo;
import me.zhengjie.modules.ejweixin.domain.NgStorefrontPhoto;
import me.zhengjie.modules.ejweixin.domain.vo.NgConstructionSiteInfoQueryCriteria;
import me.zhengjie.modules.ejweixin.domain.vo.NgStorefrontPhotoQueryCriteria;
import me.zhengjie.modules.ejweixin.service.NgCaseInfoService;
import me.zhengjie.modules.ejweixin.service.NgConstructionSiteInfoService;
import me.zhengjie.modules.ejweixin.service.NgStorefrontPhotoService;
import me.zhengjie.modules.xiao.domain.Teacher;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.xiao.service.TeacherService;
import me.zhengjie.modules.xiao.domain.vo.TeacherQueryCriteria;
import me.zhengjie.modules.xiao.mapper.TeacherMapper;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author cg
* @date 2025-05-20
**/
@Service
@RequiredArgsConstructor
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    private final TeacherMapper teacherMapper;

    @Autowired
    private NgStorefrontPhotoService ngStorefrontPhotoService;

    @Autowired
    private NgCaseInfoService ngCaseInfoService;

    @Autowired
    private NgConstructionSiteInfoService ngConstructionSiteInfoService;


    @Override
    public PageResult<Teacher> queryAll(TeacherQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(teacherMapper.findAll(criteria, page));
    }

    @Override
    public List<Teacher> queryAll(TeacherQueryCriteria criteria){
        return teacherMapper.findAll(criteria);
    }

    @Override
    public List<Teacher> getTeacherList(String name) {
        return teacherMapper.getTeacherList(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Teacher resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Teacher resources) {
        Teacher teacher = getById(resources.getId());
        teacher.copy(resources);
        saveOrUpdate(teacher);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Teacher> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Teacher teacher : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("姓名", teacher.getName());
            map.put("手机号", teacher.getPhone());
            map.put("状态", teacher.getPost());
            map.put("照片", teacher.getImg());
            map.put("案例数", teacher.getCaseNum());
            map.put("工地数", teacher.getSiteNum());
            map.put("预约数", teacher.getReseNum());
            map.put("所属门店", teacher.getRese());
            map.put("设计经验", teacher.getExperience());
            map.put("设计理念", teacher.getConcept());
            map.put("擅长风格", teacher.getStyle());
            map.put("代表作品", teacher.getWorkNum());
            map.put("创建者", teacher.getCreateBy());
            map.put("更新者", teacher.getUpdateBy());
            map.put("创建日期", teacher.getCreateTime());
            map.put("更新时间", teacher.getUpdateTime());
            map.put("所属区县", teacher.getDistrict());
            map.put("人气", teacher.getPopularity());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Map<String, Object> findTeacherByDistrict(String district) {
        Map res = new HashMap<String, Object>();
        List<Teacher> teacher = teacherMapper.findTeacherByDistrict(district);
        if(null != teacher && teacher.size() != 0){
            res.put("teacher", teacher);
            List<NgStorefrontPhoto> StorefrontPhotoList = ngStorefrontPhotoService.getImgList(district);
            res.put("storefrontPhotoList", StorefrontPhotoList);
            List<NgCaseInfo> ngCaseList = ngCaseInfoService.queryNgCaseInfoByDistrict(district, null);
            res.put("caseList", ngCaseList);
            NgConstructionSiteInfoQueryCriteria query = new NgConstructionSiteInfoQueryCriteria();
            //query.setTeacherId(teacher.get(0).getId().toString());
            query.setDistrict(district);
            List<NgConstructionSiteInfo> constructionSite = ngConstructionSiteInfoService.queryAll(query);
            res.put("constructionSite", constructionSite);
            return res;
        }else{
            List resListNull = new ArrayList<>();
            res.put("teacher", resListNull);
            res.put("storefrontPhotoList", resListNull);
            res.put("caseList", resListNull);
            res.put("constructionSite", resListNull);
            return res;
        }
    }

    @Override
    public Teacher getTeacherById(int id) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            teacherMapper.updateViewCount(String.valueOf(id));
            return teacherMapper.queryById(String.valueOf(id), userId);
        }catch (BadRequestException e){
            teacherMapper.updateViewCount(String.valueOf(id));
            return teacherMapper.queryById(String.valueOf(id), null);
        }
    }
}